package com.shushan.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.Session;
import com.shushan.versionupdate.utils.MD5Util;


/**
 * @ClassName: ChildNameSetActivity
 * @Desciption: 更改名字
 * @author: yichaohua
 * @date: 2018-08-13
 */
public class ChildNameSetActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "ChildNameSetActivity";


    private EditText nameEdit;
    private Button confirmButton;

    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.child_name_set_activity);

        setTitle("更改名字");

        initView();
    }

    private void initView() {
        nameEdit = findViewById(R.id.child_name_set_edit);
        confirmButton = findViewById(R.id.child_name_set_confirm_btn);
        confirmButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.child_name_set_confirm_btn:
                if(valide()){
                    setName();
                }
                break;
            default:
                break;
        }
    }

    private boolean valide() {
        String name = nameEdit.getText().toString();
        if(TextUtils.isEmpty(name)){
            showToast("请输入更改的姓名");
            return false;
        }

        return true;
    }

    private void setName() {
        showWaitDialog();

        int childId = babyApplication.getCurrentChildId();
        final String nickname = nameEdit.getText().toString();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_CHILD_NICK);
        urlParse.putValue("child_id", childId)
                .putValue("nickname", nickname)
                .putValue("sign", MD5Util.MD5LOW(childId + "sszx" + nickname));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        showToast("姓名修改成功");

                        Session.getSession().put("nickname",nickname);
                        onBackAction(200);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }
}
